home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite / RCS / io.c,v < prev    next >
Encoding:
Text File  |  1991-10-19  |  20.1 KB  |  840 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     91.10.19.13.29.21;  author mendel;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     90.02.14.19.25.19;  author tve;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @Original X11R4 distribution
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @*** empty log message ***
  28. @
  29. text
  30. @/***********************************************************
  31. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  32. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  33.  
  34.                         All Rights Reserved
  35.  
  36. Permission to use, copy, modify, and distribute this software and its 
  37. documentation for any purpose and without fee is hereby granted, 
  38. provided that the above copyright notice appear in all copies and that
  39. both that copyright notice and this permission notice appear in 
  40. supporting documentation, and that the names of Digital or MIT not be
  41. used in advertising or publicity pertaining to distribution of the
  42. software without specific, written prior permission.  
  43.  
  44. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  45. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  46. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  47. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  48. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  49. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  50. SOFTWARE.
  51.  
  52. ******************************************************************/
  53. /* $XConsortium: io.c,v 1.65 89/09/14 16:19:50 rws Exp $ */
  54. /*****************************************************************
  55.  * i/o functions
  56.  *
  57.  *   WriteToClient, ReadRequestFromClient
  58.  *   InsertFakeRequest, ResetCurrentRequest
  59.  *
  60.  *****************************************************************/
  61.  
  62. #include <stdio.h>
  63. #include "Xos.h"
  64. #include "Xmd.h"
  65. #include <errno.h>
  66. #include <sys/param.h>
  67. #include <sys/uio.h>
  68. #include "X.h"
  69. #include "Xproto.h"
  70. #include "os.h"
  71. #include "osdep.h"
  72. #include "opaque.h"
  73. #include "dixstruct.h"
  74. #include "misc.h"
  75.  
  76. extern void MarkClientException();
  77. extern long ClientsWithInput[];
  78. extern long ClientsWriteBlocked[];
  79. extern long OutputPending[];
  80. extern long OutputBufferSize;
  81. extern int ConnectionTranslation[];
  82. extern Bool NewOutputPending;
  83. extern Bool AnyClientsWriteBlocked;
  84. static Bool CriticalOutputPending;
  85. static int timesThisConnection = 0;
  86. static ConnectionInputPtr FreeInputs = (ConnectionInputPtr)NULL;
  87. static ConnectionOutputPtr FreeOutputs = (ConnectionOutputPtr)NULL;
  88. static OsCommPtr AvailableInput = (OsCommPtr)NULL;
  89.  
  90. static ConnectionInputPtr AllocateInputBuffer();
  91. static ConnectionOutputPtr AllocateOutputBuffer();
  92.  
  93. extern int errno;
  94.  
  95. #define request_length(req, cli) ((cli->swapped ? \
  96.     lswaps((req)->length) : (req)->length) << 2)
  97. #define MAX_TIMES_PER         10
  98.  
  99. /*****************************************************************
  100.  * ReadRequestFromClient
  101.  *    Returns one request in client->requestBuffer.  Return status is:
  102.  *
  103.  *    > 0  if  successful, specifies length in bytes of the request
  104.  *    = 0  if  entire request is not yet available
  105.  *    < 0  if  client should be terminated
  106.  *
  107.  *    The request returned must be contiguous so that it can be
  108.  *    cast in the dispatcher to the correct request type.  Because requests
  109.  *    are variable length, ReadRequestFromClient() must look at the first 4
  110.  *    bytes of a request to determine the length (the request length is
  111.  *    always the 3rd and 4th bytes of the request).  
  112.  *
  113.  *    Note: in order to make the server scheduler (WaitForSomething())
  114.  *    "fair", the ClientsWithInput mask is used.  This mask tells which
  115.  *    clients have FULL requests left in their buffers.  Clients with
  116.  *    partial requests require a read.  Basically, client buffers
  117.  *    are drained before select() is called again.  But, we can't keep
  118.  *    reading from a client that is sending buckets of data (or has
  119.  *    a partial request) because others clients need to be scheduled.
  120.  *****************************************************************/
  121.  
  122. #define YieldControl()                \
  123.         { isItTimeToYield = TRUE;        \
  124.       timesThisConnection = 0; }
  125. #define YieldControlNoInput()            \
  126.         { YieldControl();            \
  127.       BITCLEAR(ClientsWithInput, fd); }
  128. #define YieldControlDeath()            \
  129.         { timesThisConnection = 0; }
  130.  
  131. int
  132. ReadRequestFromClient(client)
  133.     ClientPtr client;
  134. {
  135.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  136.     register ConnectionInputPtr oci = oc->input;
  137.     int fd = oc->fd;
  138.     int result, gotnow, needed;
  139.     register xReq *request;
  140.  
  141.     if (AvailableInput)
  142.     {
  143.     if (AvailableInput != oc)
  144.     {
  145.         AvailableInput->input->next = FreeInputs;
  146.         FreeInputs = AvailableInput->input;
  147.         AvailableInput->input = (ConnectionInputPtr)NULL;
  148.     }
  149.     AvailableInput = (OsCommPtr)NULL;
  150.     }
  151.     if (!oci)
  152.     {
  153.     if (oci = FreeInputs)
  154.     {
  155.         FreeInputs = oci->next;
  156.     }
  157.     else if (!(oci = AllocateInputBuffer()))
  158.     {
  159.         YieldControlDeath();
  160.         return -1;
  161.     }
  162.     oc->input = oci;
  163.     }
  164.     oci->bufptr += oci->lenLastReq;
  165.  
  166.     request = (xReq *)oci->bufptr;
  167.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  168.     if ((gotnow < sizeof(xReq)) ||
  169.     (gotnow < (needed = request_length(request, client))))
  170.     {
  171.     oci->lenLastReq = 0;
  172.     if ((gotnow < sizeof(xReq)) || (needed == 0))
  173.        needed = sizeof(xReq);
  174.     else if (needed > MAXBUFSIZE)
  175.     {
  176.         YieldControlDeath();
  177.         return -1;
  178.     }
  179.     if ((gotnow == 0) ||
  180.         ((oci->bufptr - oci->buffer + needed) > oci->size))
  181.     {
  182.         if ((gotnow > 0) && (oci->bufptr != oci->buffer))
  183.         bcopy(oci->bufptr, oci->buffer, gotnow);
  184.         if (needed > oci->size)
  185.         {
  186.         char *ibuf;
  187.  
  188.         ibuf = (char *)xrealloc(oci->buffer, needed);
  189.         if (!ibuf)
  190.         {
  191.             YieldControlDeath();
  192.             return -1;
  193.         }
  194.         oci->size = needed;
  195.         oci->buffer = ibuf;
  196.         }
  197.         oci->bufptr = oci->buffer;
  198.         oci->bufcnt = gotnow;
  199.     }
  200. #ifdef SPRITEPDEVCONN
  201.     if (PdevIsPdevConn(fd)) {
  202.         result = PdevRead(fd, oci->buffer + oci->bufcnt, 
  203.               oci->size - oci->bufcnt); 
  204.     } else
  205. #endif
  206.     result = read(fd, oci->buffer + oci->bufcnt, 
  207.               oci->size - oci->bufcnt); 
  208.     if (result <= 0)
  209.     {
  210.         if ((result < 0) && (errno == EWOULDBLOCK))
  211.         {
  212.         YieldControlNoInput();
  213.         return 0;
  214.         }
  215.         YieldControlDeath();
  216.         return -1;
  217.     }
  218.     oci->bufcnt += result;
  219.     gotnow += result;
  220.     /* free up some space after huge requests */
  221.     if ((oci->size > BUFWATERMARK) &&
  222.         (oci->bufcnt < BUFSIZE) && (needed < BUFSIZE))
  223.     {
  224.         char *ibuf;
  225.  
  226.         ibuf = (char *)xrealloc(oci->buffer, BUFSIZE);
  227.         if (ibuf)
  228.         {
  229.         oci->size = BUFSIZE;
  230.         oci->buffer = ibuf;
  231.         oci->bufptr = ibuf + oci->bufcnt - gotnow;
  232.         }
  233.     }
  234.     request = (xReq *)oci->bufptr;
  235.     if ((gotnow < sizeof(xReq)) ||
  236.         (gotnow < (needed = request_length(request, client))))
  237.     {
  238.         YieldControlNoInput();
  239.         return 0;
  240.     }
  241.     }
  242.     if (needed == 0)
  243.     needed = sizeof(xReq);
  244.     oci->lenLastReq = needed;
  245.  
  246.     /*
  247.      *  Check to see if client has at least one whole request in the
  248.      *  buffer.  If there is only a partial request, treat like buffer
  249.      *  is empty so that select() will be called again and other clients
  250.      *  can get into the queue.   
  251.      */
  252.  
  253.     if (gotnow >= needed + sizeof(xReq)) 
  254.     {
  255.     request = (xReq *)(oci->bufptr + needed);
  256.         if (gotnow >= needed + request_length(request, client))
  257.         BITSET(ClientsWithInput, fd);
  258.         else
  259.         YieldControlNoInput();
  260.     }
  261.     else
  262.     {
  263.     if (gotnow == needed)
  264.         AvailableInput = oc;
  265.     YieldControlNoInput();
  266.     }
  267.     if (++timesThisConnection >= MAX_TIMES_PER)
  268.     YieldControl();
  269.  
  270.     client->requestBuffer = (pointer)oci->bufptr;
  271.     return needed;
  272. }
  273.  
  274. /*****************************************************************
  275.  * InsertFakeRequest
  276.  *    Splice a consed up (possibly partial) request in as the next request.
  277.  *
  278.  **********************/
  279.  
  280. Bool
  281. InsertFakeRequest(client, data, count)
  282.     ClientPtr client;
  283.     char *data;
  284.     int count;
  285. {
  286.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  287.     register ConnectionInputPtr oci = oc->input;
  288.     int fd = oc->fd;
  289.     register xReq *request;
  290.     int gotnow, moveup;
  291.  
  292.     if (AvailableInput)
  293.     {
  294.     if (AvailableInput != oc)
  295.     {
  296.         AvailableInput->input->next = FreeInputs;
  297.         FreeInputs = AvailableInput->input;
  298.         AvailableInput->input = (ConnectionInputPtr)NULL;
  299.     }
  300.     AvailableInput = (OsCommPtr)NULL;
  301.     }
  302.     if (!oci)
  303.     {
  304.     if (oci = FreeInputs)
  305.         FreeInputs = oci->next;
  306.     else if (!(oci = AllocateInputBuffer()))
  307.         return FALSE;
  308.     oc->input = oci;
  309.     }
  310.     oci->bufptr += oci->lenLastReq;
  311.     oci->lenLastReq = 0;
  312.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  313.     if ((gotnow + count) > oci->size)
  314.     {
  315.     char *ibuf;
  316.  
  317.     ibuf = (char *)xrealloc(oci->buffer, gotnow + count);
  318.     if (!ibuf)
  319.         return(FALSE);
  320.     oci->size = gotnow + count;
  321.     oci->buffer = ibuf;
  322.     oci->bufptr = ibuf + oci->bufcnt - gotnow;
  323.     }
  324.     moveup = count - (oci->bufptr - oci->buffer);
  325.     if (moveup > 0)
  326.     {
  327.     if (gotnow > 0)
  328.         bcopy(oci->bufptr, oci->bufptr + moveup, gotnow);
  329.     oci->bufptr += moveup;
  330.     oci->bufcnt += moveup;
  331.     }
  332.     bcopy(data, oci->bufptr - count, count);
  333.     oci->bufptr -= count;
  334.     request = (xReq *)oci->bufptr;
  335.     gotnow += count;
  336.     if ((gotnow >= sizeof(xReq)) &&
  337.     (gotnow >= request_length(request, client)))
  338.     BITSET(ClientsWithInput, fd);
  339.     else
  340.     YieldControlNoInput();
  341.     return(TRUE);
  342. }
  343.  
  344. /*****************************************************************
  345.  * ResetRequestFromClient
  346.  *    Reset to reexecute the current request, and yield.
  347.  *
  348.  **********************/
  349.  
  350. ResetCurrentRequest(client)
  351.     ClientPtr client;
  352. {
  353.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  354.     register ConnectionInputPtr oci = oc->input;
  355.     int fd = oc->fd;
  356.     register xReq *request;
  357.     int gotnow;
  358.  
  359.     if (AvailableInput == oc)
  360.     AvailableInput = (OsCommPtr)NULL;
  361.     oci->lenLastReq = 0;
  362.     request = (xReq *)oci->bufptr;
  363.     gotnow = oci->bufcnt + oci->buffer - oci->bufptr;
  364.     if ((gotnow >= sizeof(xReq)) &&
  365.     (gotnow >= request_length(request, client)))
  366.     {
  367.     BITSET(ClientsWithInput, fd);
  368.     YieldControl();
  369.     }
  370.     else
  371.     YieldControlNoInput();
  372. }
  373.  
  374.     /* lookup table for adding padding bytes to data that is read from
  375.         or written to the X socket.  */
  376. static int padlength[4] = {0, 3, 2, 1};
  377.  
  378.  /********************
  379.  * FlushClient()
  380.  *    If the client isn't keeping up with us, then we try to continue
  381.  *    buffering the data and set the apropriate bit in ClientsWritable
  382.  *    (which is used by WaitFor in the select).  If the connection yields
  383.  *    a permanent error, or we can't allocate any more space, we then
  384.  *    close the connection.
  385.  *
  386.  **********************/
  387.  
  388. int
  389. FlushClient(who, oc, extraBuf, extraCount)
  390.     ClientPtr who;
  391.     OsCommPtr oc;
  392.     char *extraBuf;
  393.     int extraCount; /* do not modify... returned below */
  394. {
  395.     register ConnectionOutputPtr oco = oc->output;
  396.     int connection = oc->fd;
  397.     struct iovec iov[3];
  398.     char padBuffer[3];
  399.     long written;
  400.     long padsize;
  401.     long notWritten;
  402.     long todo;
  403.  
  404.     if (!oco)
  405.     return 0;
  406.     written = 0;
  407.     padsize = padlength[extraCount & 3];
  408.     notWritten = oco->count + extraCount + padsize;
  409.     todo = notWritten;
  410.     while (notWritten) {
  411.     long before = written;    /* amount of whole thing written */
  412.     long remain = todo;    /* amount to try this time, <= notWritten */
  413.     int i = 0;
  414.     long len;
  415.  
  416.     /* You could be very general here and have "in" and "out" iovecs
  417.      * and write a loop without using a macro, but what the heck.  This
  418.      * translates to:
  419.      *
  420.      *     how much of this piece is new?
  421.      *     if more new then we are trying this time, clamp
  422.      *     if nothing new
  423.      *         then bump down amount already written, for next piece
  424.      *         else put new stuff in iovec, will need all of next piece
  425.      *
  426.      * Note that todo had better be at least 1 or else we'll end up
  427.      * writing 0 iovecs.
  428.      */
  429. #define InsertIOV(pointer, length) \
  430.     len = (length) - before; \
  431.     if (len > remain) \
  432.         len = remain; \
  433.     if (len <= 0) { \
  434.         before = (-len); \
  435.     } else { \
  436.         iov[i].iov_len = len; \
  437.         iov[i].iov_base = (pointer) + before; \
  438.         i++; \
  439.         remain -= len; \
  440.         before = 0; \
  441.     }
  442.  
  443.     InsertIOV ((char *)oco->buf, oco->count)
  444.     InsertIOV (extraBuf, extraCount)
  445.     InsertIOV (padBuffer, padsize)
  446.  
  447.     errno = 0;
  448. #ifdef SPRITEPDEVCONN
  449.     if (PdevIsPdevConn(connection)) {
  450.         len = PdevWritev(connection, iov, i);
  451.     } else { 
  452.         len = writev(connection, iov, i);
  453.     }
  454.     if (len >= 0)
  455. #else
  456.     if ((len = writev(connection, iov, i)) >= 0)
  457. #endif
  458.     {
  459.         written += len;
  460.         notWritten -= len;
  461.         todo = notWritten;
  462.     }
  463. #ifdef apollo /* stupid sr10.1 UDS bug - supposedly fixed in sr10.2 */
  464.     else if ((errno == EWOULDBLOCK) && (todo > 4096))
  465.     {
  466.         todo = 4096;
  467.     }
  468. #endif
  469.     else if ((errno == EWOULDBLOCK)
  470. #ifdef SUNSYSV /* check for another brain-damaged OS bug */
  471.          || (errno == 0)
  472. #endif
  473. #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
  474.          || ((errno == EMSGSIZE) && (todo == 1))
  475. #endif
  476.         )
  477.     {
  478.         /* If we've arrived here, then the client is stuffed to the gills
  479.            and not ready to accept more.  Make a note of it and buffer
  480.            the rest. */
  481.         BITSET(ClientsWriteBlocked, connection);
  482.         AnyClientsWriteBlocked = TRUE;
  483.  
  484.         if (written < oco->count)
  485.         {
  486.         if (written > 0)
  487.         {
  488.             oco->count -= written;
  489.             bcopy((char *)oco->buf + written,
  490.               (char *)oco->buf,
  491.               oco->count);
  492.             written = 0;
  493.         }
  494.         }
  495.         else
  496.         {
  497.         written -= oco->count;
  498.         oco->count = 0;
  499.         }
  500.  
  501.         if (notWritten > oco->size)
  502.         {
  503.         unsigned char *obuf;
  504.  
  505.         obuf = (unsigned char *)xrealloc(oco->buf,
  506.                          notWritten +
  507.                          OutputBufferSize);
  508.         if (!obuf)
  509.         {
  510.             close(connection);
  511.             MarkClientException(who);
  512.             oco->count = 0;
  513.             return(-1);
  514.         }
  515.         oco->size = notWritten + OutputBufferSize;
  516.         oco->buf = obuf;
  517.         }
  518.  
  519.         /* If the amount written extended into the padBuffer, then the
  520.            difference "extraCount - written" may be less than 0 */
  521.         if ((len = extraCount - written) > 0)
  522.         bcopy (extraBuf + written,
  523.                (char *)oco->buf + oco->count,
  524.                len);
  525.  
  526.         oco->count = notWritten; /* this will include the pad */
  527.         /* return only the amount explicitly requested */
  528.         return extraCount;
  529.     }
  530. #ifdef EMSGSIZE /* check for another brain-damaged OS bug */
  531.     else if (errno == EMSGSIZE)
  532.     {
  533.         todo >>= 1;
  534.     }
  535. #endif
  536.     else
  537.     {
  538. #ifdef SPRITEPDEVCONN
  539.         if (PdevIsPdevConn(connection)) {
  540.         PdevClose(connection);
  541.         } else
  542. #endif
  543.         close(connection);
  544.         MarkClientException(who);
  545.         oco->count = 0;
  546.         return(-1);
  547.     }
  548.     }
  549.  
  550.     /* everything was flushed out */
  551.     oco->count = 0;
  552.     /* check to see if this client was write blocked */
  553.     if (AnyClientsWriteBlocked)
  554.     {
  555.     BITCLEAR(ClientsWriteBlocked, oc->fd);
  556.     if (! ANYSET(ClientsWriteBlocked))
  557.         AnyClientsWriteBlocked = FALSE;
  558.     }
  559.     if (oco->size > OutputBufferSize)
  560.     {
  561.     unsigned char *obuf;
  562.  
  563.     obuf = (unsigned char *)xrealloc(oco->buf, OutputBufferSize);
  564.     if (obuf)
  565.     {
  566.         oco->size = OutputBufferSize;
  567.         oco->buf = obuf;
  568.     }
  569.     }
  570.     oco->next = FreeOutputs;
  571.     FreeOutputs = oco;
  572.     oc->output = (ConnectionOutputPtr)NULL;
  573.     return extraCount; /* return only the amount explicitly requested */
  574. }
  575.  
  576.  /********************
  577.  * FlushAllOutput()
  578.  *    Flush all clients with output.  However, if some client still
  579.  *    has input in the queue (more requests), then don't flush.  This
  580.  *    will prevent the output queue from being flushed every time around
  581.  *    the round robin queue.  Now, some say that it SHOULD be flushed
  582.  *    every time around, but...
  583.  *
  584.  **********************/
  585.  
  586. void
  587. FlushAllOutput()
  588. {
  589.     register int index, base, mask;
  590.     OsCommPtr oc;
  591.     register ClientPtr client;
  592.  
  593.     if (! NewOutputPending)
  594.     return;
  595.  
  596.     /*
  597.      * It may be that some client still has critical output pending,
  598.      * but he is not yet ready to receive it anyway, so we will
  599.      * simply wait for the select to tell us when he's ready to receive.
  600.      */
  601.     CriticalOutputPending = FALSE;
  602.     NewOutputPending = FALSE;
  603.  
  604.     for (base = 0; base < mskcnt; base++)
  605.     {
  606.     mask = OutputPending[ base ];
  607.     OutputPending[ base ] = 0;
  608.     while (mask)
  609.     {
  610.         index = ffs(mask) - 1;
  611.         mask &= ~lowbit(mask);
  612.         if ((index = ConnectionTranslation[(base << 5) + index]) == 0)
  613.         continue;
  614.         client = clients[index];
  615.         if (client->clientGone)
  616.         continue;
  617.         oc = (OsCommPtr)client->osPrivate;
  618.         if (GETBIT(ClientsWithInput, oc->fd))
  619.         {
  620.         BITSET(OutputPending, oc->fd); /* set the bit again */
  621.         NewOutputPending = TRUE;
  622.         }
  623.         else
  624.         (void)FlushClient(client, oc, (char *)NULL, 0);
  625.     }
  626.     }
  627.  
  628. }
  629.  
  630. void
  631. FlushIfCriticalOutputPending()
  632. {
  633.     if (CriticalOutputPending)
  634.     FlushAllOutput();
  635. }
  636.  
  637. void
  638. SetCriticalOutputPending()
  639. {
  640.     CriticalOutputPending = TRUE;
  641. }
  642.  
  643. /*****************
  644.  * WriteToClient
  645.  *    Copies buf into ClientPtr.buf if it fits (with padding), else
  646.  *    flushes ClientPtr.buf and buf to client.  As of this writing,
  647.  *    every use of WriteToClient is cast to void, and the result
  648.  *    is ignored.  Potentially, this could be used by requests
  649.  *    that are sending several chunks of data and want to break
  650.  *    out of a loop on error.  Thus, we will leave the type of
  651.  *    this routine as int.
  652.  *****************/
  653.  
  654. int
  655. WriteToClient (who, count, buf)
  656.     ClientPtr who;
  657.     char *buf;
  658.     int count;
  659. {
  660.     OsCommPtr oc = (OsCommPtr)who->osPrivate;
  661.     register ConnectionOutputPtr oco = oc->output;
  662.     int padBytes;
  663.  
  664.     if (!count)
  665.     return(0);
  666.  
  667.     if (!oco)
  668.     {
  669.     if (oco = FreeOutputs)
  670.     {
  671.         FreeOutputs = oco->next;
  672.     }
  673.     else if (!(oco = AllocateOutputBuffer()))
  674.     {
  675. #ifdef SPRITEPDEVCONN
  676.         if (PdevIsPdevConn(oc->fd)) {
  677.         PdevClose(oc->fd);
  678.         } else
  679. #endif
  680.         close(oc->fd);
  681.         MarkClientException(who);
  682.         return -1;
  683.     }
  684.     oc->output = oco;
  685.     }
  686.  
  687.     padBytes =  padlength[count & 3];
  688.  
  689.     if (oco->count + count + padBytes > oco->size)
  690.     {
  691.     BITCLEAR(OutputPending, oc->fd);
  692.     CriticalOutputPending = FALSE;
  693.     NewOutputPending = FALSE;
  694.     return FlushClient(who, oc, buf, count);
  695.     }
  696.  
  697.     NewOutputPending = TRUE;
  698.     BITSET(OutputPending, oc->fd);
  699.     bcopy(buf, (char *)oco->buf + oco->count, count);
  700.     oco->count += count + padBytes;
  701.     
  702.     return(count);
  703. }
  704.  
  705. static ConnectionInputPtr
  706. AllocateInputBuffer()
  707. {
  708.     register ConnectionInputPtr oci;
  709.  
  710.     oci = (ConnectionInputPtr)xalloc(sizeof(ConnectionInput));
  711.     if (!oci)
  712.     return (ConnectionInputPtr)NULL;
  713.     oci->buffer = (char *)xalloc(BUFSIZE);
  714.     if (!oci->buffer)
  715.     {
  716.     xfree(oci);
  717.     return (ConnectionInputPtr)NULL;
  718.     }
  719.     oci->size = BUFSIZE;
  720.     oci->bufptr = oci->buffer;
  721.     oci->bufcnt = 0;
  722.     oci->lenLastReq = 0;
  723.     return oci;
  724. }
  725.  
  726. static ConnectionOutputPtr
  727. AllocateOutputBuffer()
  728. {
  729.     register ConnectionOutputPtr oco;
  730.  
  731.     oco = (ConnectionOutputPtr)xalloc(sizeof(ConnectionOutput));
  732.     if (!oco)
  733.     return (ConnectionOutputPtr)NULL;
  734.     oco->buf = (unsigned char *) xalloc(OutputBufferSize);
  735.     if (!oco->buf)
  736.     {
  737.     xfree(oco);
  738.     return (ConnectionOutputPtr)NULL;
  739.     }
  740.     oco->size = OutputBufferSize;
  741.     oco->count = 0;
  742.     return oco;
  743. }
  744.  
  745. void
  746. FreeOsBuffers(oc)
  747.     OsCommPtr oc;
  748. {
  749.     register ConnectionInputPtr oci;
  750.     register ConnectionOutputPtr oco;
  751.  
  752.     if (AvailableInput == oc)
  753.     AvailableInput = (OsCommPtr)NULL;
  754.     if (oci = oc->input)
  755.     {
  756.     if (FreeInputs)
  757.     {
  758.         xfree(oci->buffer);
  759.         xfree(oci);
  760.     }
  761.     else
  762.     {
  763.         FreeInputs = oci;
  764.         oci->next = (ConnectionInputPtr)NULL;
  765.         oci->bufptr = oci->buffer;
  766.         oci->bufcnt = 0;
  767.         oci->lenLastReq = 0;
  768.     }
  769.     }
  770.     if (oco = oc->output)
  771.     {
  772.     if (FreeOutputs)
  773.     {
  774.         xfree(oco->buf);
  775.         xfree(oco);
  776.     }
  777.     else
  778.     {
  779.         FreeOutputs = oco;
  780.         oco->next = (ConnectionOutputPtr)NULL;
  781.         oco->count = 0;
  782.     }
  783.     }
  784. }
  785.  
  786. void
  787. ResetOsBuffers()
  788. {
  789.     register ConnectionInputPtr oci;
  790.     register ConnectionOutputPtr oco;
  791.  
  792.     while (oci = FreeInputs)
  793.     {
  794.     FreeInputs = oci->next;
  795.     xfree(oci->buffer);
  796.     xfree(oci);
  797.     }
  798.     while (oco = FreeOutputs)
  799.     {
  800.     FreeOutputs = oco->next;
  801.     xfree(oco->buf);
  802.     xfree(oco);
  803.     }
  804. }
  805. #ifdef SPRITEPDEVCONN
  806. /*
  807.  * This is needed until select() on a pdev returns when it is writable.
  808.  */
  809. PdevClearWriteBlockHack(fd)
  810.     int fd;
  811. {
  812.     if (AnyClientsWriteBlocked && GETBIT(ClientsWriteBlocked,fd))
  813.     {
  814.     NewOutputPending = TRUE;
  815.     BITSET(OutputPending, fd);
  816.     BITCLEAR(ClientsWriteBlocked, fd);
  817.     if (! ANYSET(ClientsWriteBlocked))
  818.         AnyClientsWriteBlocked = FALSE;
  819.     }
  820. }
  821. #endif
  822. @
  823.  
  824.  
  825. 1.1
  826. log
  827. @Initial revision
  828. @
  829. text
  830. @d171 6
  831. d419 8
  832. d428 1
  833. d509 5
  834. d527 1
  835. a527 1
  836.      if (! ANYSET(ClientsWriteBlocked))
  837. d646 5
  838. d776 17
  839. @
  840.